﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
using System.Xml;

namespace Pikapika.Controllers
{
    public class FeedResult : ActionResult
    {
        SyndicationFeed _feed;

        public FeedResult(SyndicationFeed feed)
        {
            if (feed == null)
                throw new ArgumentNullException("feed", "feed is null.");
            this._feed = feed;
        }

        public SyndicationFeed Feed
        {
            get
            {
                return _feed;
            }
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context", "context is null.");

            context.HttpContext.Response.ContentType = "application/atom+xml";
            using (var writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                new Atom10FeedFormatter(Feed).WriteTo(writer);
            }
            
        }
    }
}